module.exports = {
    state: {
        complaints: {},
        pubComplaints: {}
    },
    mutations: {
        complaintsList (state, data) {
            state.complaints = {
                list: _.sortBy(data, 'comp_date')
            }
        },
        pubComplaintsList (state, data) {
            state.pubComplaints = {
                list: data
            }
        },
        updateComplaints (state, data) {
            const rec = data[0]
            if(rec) {
                state.complaints = {
                    list: _.map(state.complaints.list, (item) => {
                        return item.comp_id == rec.comp_id ? rec : item
                    })
                }
            }
        }
    },
    actions: {
        getComplaints ({commit, state}, {channel, resolve}) {
            $store.api.call('complaints_list', {
                message: '获取投诉建议列表失败',
                params: {id: channel}
            }, ({data}) => {
                commit('complaintsList', data)
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        getPubComplaints ({commit, state}, {channel, resolve}) {
            $store.api.call('complaints_pub', {
                message: '获取投诉建议列表失败',
                params: {id: channel}
            }, ({data}) => {
                commit('pubComplaintsList', data)
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        newComplaints ({commit, state}, {channel, title, content, dept, resolve}) {
            $store.api.call('complaints_new', {
                message: '发送投诉建议失败',
                params: {id: channel},
                form: {
                    title, content, dept
                }
            }, ({data}) => {
                commit('complaintsList', data)
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        },
        replyComplaints ({commit, state}, {id, content, solved, resolve}) {
            $store.api.call('complaints_reply', {
                message: '回复投诉建议失败',
                params: {
                    id
                },
                form: {
                    content, solved
                }
            }, ({data}) => {
                commit('updateComplaints', data)
                if(resolve && resolve.call) {
                    resolve(data)
                }
            })
        }
    },
    getters: {
        complaints (state) {
            return state.complaints
        },
        pubComplaints (state) {
            return state.pubComplaints
        }
    }
}